package com.ts.api.http.market.channel;

import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.date.parse.DateParser;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.util.ApiDictUtils;
import com.ts.api.http.market.channel.entity.ChannelHuiYiHua;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarket;
import com.ts.api.module.api.convert.core.annotation.ApiChannelMarketConvertType;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.market.entity.ChannelMarketParameter;
import com.ts.api.module.api.convert.market.handler.ChannelMarketConvertHandler;
import com.ts.api.module.api.entity.ApiUserRequest;

import java.time.LocalDate;
import java.util.Map;

/**
 *  惠逸花-撞库表单
 *
 *  @author Pan
 */
@ApiChannelMarket(uniqueCode = "4fc536cbd4cc400a9a2bd6fd79eea32d", type = ApiChannelMarketConvertType.DATA_EXCHANGE_FORM)
public class HuiYihuaForm implements ChannelMarketConvertHandler {

    public static final String KEY = "tnl3x3nwzi8z2ey2";
    public static final String IV = "tnl3x3nwzi8z2ey2";

    @Override
    public ApiUserRequest parse(RequestVO requestVO) {
        String data = requestVO.getStr("data");
        AES cbc = AES.createCbc(KEY.getBytes(), IV.getBytes());
        String decrypt = cbc.decryptFromBase64Str(data);

        ChannelHuiYiHua bean = JsonUtils.toBean(decrypt, ChannelHuiYiHua.class);
        ApiUserRequest apiUserRequest = CopyUtils.copy(bean, ApiUserRequest.class);
        fieldMapping(apiUserRequest);
        return apiUserRequest;
    }

    @Override
    public Map<String, Object> response(ChannelMarketParameter channelMarketParameter, ApiBizStatus apiBizStatus) {
        ApiUserRequest apiUserRequest = channelMarketParameter.getApiUserRequest();
        //  返回失败及验证是否芝麻分650以上
        if (!apiBizStatus.hasBizSuc() || !Eq.strOr(apiUserRequest.getSesameCredit(), "3", "4")) {
            return MapUtils.newHashMap(newMap -> {
                newMap.put("code", 1000);
                newMap.put("msg", "撞库失败");
            });
        }

        //  返回成功
        return MapUtils.newHashMap(newMap -> {
            newMap.put("code", 200);
            newMap.put("msg", "撞库资质成功");
        });
    }

    public static void fieldMapping(ApiUserRequest apiUserRequest) {
        String age = apiUserRequest.getAge();

        //  获取年龄
        LocalDate userDate = DateParser.getLocalDate(age);
        LocalDate nowDate = DateParser.getLocalDate(DateUtils.currentTime("yyyy-MM-dd"));
        int ageInt = userDate.until(nowDate).getYears();
        apiUserRequest.setAge(String.valueOf(ageInt));

        //  demandAmount	int	是	需求金额（单位：元）
        String loanAmount = ApiDictUtils.convertLoanAmount(apiUserRequest.getLoanAmount(), true);
        apiUserRequest.setLoanAmount(loanAmount);

        //  city	string	是	城市，如：成都市
        String city = apiUserRequest.getCity();
        apiUserRequest.setCity(city);

        //  信用情况（参考字典）
        String overdue = ChooseEq.create(apiUserRequest.getOverdue())
                .when("122", "1")
                .when("123", "1")
                .when("124", "3")
                .when("125", "2")
                .when("126", "2")
                .end();
        apiUserRequest.setOverdue(overdue);

        //  houseId	int	是	房（参考字典）
        String realEstateInfo = ChooseEq.create(apiUserRequest.getRealEstateInfo())
                .when("131", "1")
                .when("132", "6")
                .when("133", "5")
                .when("134", "4")
                .when("135", "4")
                .end();
        apiUserRequest.setRealEstateInfo(realEstateInfo);

        //  vehicleId	int	是	车（参考字典）
        String carInfo = ChooseEq.create(apiUserRequest.getCarInfo())
                .when("138", "1")
                .when("139", "3")
                .when("149", "4")
                .end();
        apiUserRequest.setCarInfo(carInfo);

        //  sesameId	int	是	芝麻分（参考字典）
        String sesameCredit = ChooseEq.create(apiUserRequest.getSesameCredit())
                .when("110", "1")
                .when("112", "2")
                .when("113", "3")
                .when("115", "4")
                .end();
        apiUserRequest.setSesameCredit(sesameCredit);

        //  vocationId	int	是	职业身份（参考字典）
        String career = ChooseEq.create(apiUserRequest.getCareer())
                .when("127", "1")
                .when("128", "3")
                .when("129", "2")
                .when("130", "5")
                .end();
        apiUserRequest.setCareer(career);

        //  socialId	int	是	社保（参考字典）
        String socialSecurity = ChooseEq.create(apiUserRequest.getSocialSecurity())
                .when("159", "2")
                .when("158", "3")
                .when("156", "1")
                .end();
        apiUserRequest.setSocialSecurity(socialSecurity);

        //  insureId	int	是	保险（参考字典）
        String insurancePolicy = ChooseEq.create(apiUserRequest.getInsurancePolicy())
                .when("147", "3")
                .when("146", "2")
                .when("145", "1")
                .end();
        apiUserRequest.setInsurancePolicy(insurancePolicy);

        //  providentId	int	是	公积金（参考字典）
        String providentFund = ChooseEq.create(apiUserRequest.getProvidentFund())
                .when("153", "3")
                .when("152", "2")
                .when("150", "1")
                .end();
        apiUserRequest.setProvidentFund(providentFund);
    }
}
